package com.example.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

//JWT的生成和验证
public class JwtUtil {

    private static final String KEY="guet";

//    接收业务数据，生成并返回token
    public static String getToken(Map<String,Object> claims)
    {
        return JWT.create()
                .withClaim("claims",claims)
                .withExpiresAt(new Date(System.currentTimeMillis()+100*60*60))
                .sign(Algorithm.HMAC256(KEY));
    }

//    接收token，验证token,并且返回业务数据
    public static Map<String,Object>parseToken(String token)
    {
        return JWT.require(Algorithm.HMAC256(KEY))
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
    }

}

